/* @(#)GSClientSocket.java    
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li     2006-2-18    新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.integration.socket;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import org.apache.log4j.Logger;

import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.lib.GSSystemConfigurator;

import com.hitrust.gs.net.HttpConnection;

/**
 * 
 */
public class GSClientSocket {
    
    /**
     * 定义socket Logger
     */
    private static final Logger logger = Logger.getLogger("socket");
    
    /**
     * 服务端IP
     */
    private String iServerIP;
    /**
     * 服务端端口
     */
    private int iServerPort;
    
    public GSClientSocket(String aServerIP,int aServerPort){
        
        iServerIP = aServerIP;
    
        iServerPort = aServerPort;
    }
   
	/**
	 * 发送信息到采购网
	 * @param aContent   待发送信息
	 * @return
	 * @throws GSException
	 */
	public String sendContents_socket(String aContent) throws GSException{
		String tRs = "";
//		  BufferedReader tIn = null;
//		  Socket tServer = null;
//		  PrintWriter tOut = null;
		DataInputStream tIn = null;
		Socket tServer = null;
		DataOutputStream tOut = null;
        
        
		try{
			//请求建立连接
			tServer = new Socket(iServerIP,iServerPort);
			GSLogWriter.debug(logger, this, "connect IP = " + iServerIP);
			GSLogWriter.debug(logger, this, "connect PORT = " + iServerPort);
            
			SocketUtil.send(tServer, aContent, "utf-8", "");
			GSLogWriter.debug(logger, this, "发送值="+aContent);
			tRs = SocketUtil.receive(tServer, "GB2312", "");
            
			GSLogWriter.debug(logger, this,"返回值="+tRs);
            
		}catch(IOException ie){
			GSLogWriter.error(logger, this,ie.toString());
			throw new GSException(GSException.E10050,GSException.E10050_MSG);
		}catch(Exception e){
			GSLogWriter.error(logger, this,e.toString());
			throw new GSException(GSException.E00001,GSException.E00001_MSG);
		}finally{
			try{
				if(tOut != null){
					tOut.close();
				}
				if(tIn != null){
					tIn.close();
				}
				if(tServer != null){
					tServer.close();
				}
			}catch(IOException e){
			   GSLogWriter.error(logger, this,e.toString()); 
			}
		}
		return tRs;
	}
     
     
     
	/**
	 * 服务端servlet名称
	 */
	private String iServletName;
    
	public GSClientSocket(String aServletName){
    
		iServletName = aServletName;
	}
    
	/**
	 * 发送信息到采购网
	 * @param aContent   待发送信息
	 * @return
	 * @throws GSException
	 */
	public String sendContents(String aContent) throws GSException
	{
		String tRs = "";
		HttpConnection htc = null;
        
        
		try{
			//请求建立连接
			htc = new HttpConnection(); //HttpConnection
            
            String mContent = aContent;
            
//			mContent = new String(mContent.getBytes(),"utf-8");
            
            //tRs = htc.doGet(mContent);
            
			String tHttp = GSSystemConfigurator.getParam("gs.HttpServletUrl");
            tRs = htc.getMessage(tHttp,mContent);
            
			GSLogWriter.debug(logger, this,"返回值="+tRs);
            
		}catch(Exception e){
			GSLogWriter.error(logger, this,e.toString());
			throw new GSException(GSException.E00001,GSException.E00001_MSG);
		}finally{
			
		}
		return tRs;
	}
    
}
